<?php
require_once 'config21.php';
if (!isset($_SESSION['username'])) {
    header('Location: login21.php');
    exit();
}

if (isset($_GET['id'])) {
    $course_id = $_GET['id'];
    $sql = "SELECT courses.*, teachers.teacher_name 
            FROM courses 
            LEFT JOIN teachers ON courses.teacher_id = teachers.teacher_id 
            WHERE course_id = $course_id";
    $result = $conn->query($sql);
    if ($result->num_rows == 1) {
        $row = $result->fetch_assoc();
    } else {
        echo "未找到该课程信息";
        exit;
    }
} else {
    echo "缺少课程 ID";
    exit;
}

// 获取所有教师列表用于下拉选择
$teachers_sql = "SELECT teacher_id, teacher_name FROM teachers";
$teachers_result = $conn->query($teachers_sql);

// 修改课程
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST['edit_course'])) {
    $course_name = $_POST['course_name'];
    $teacher_id = $_POST['teacher_id'];
    $credit = $_POST['credit'];
    
    // 验证学分
    if ($credit <= 0) {
        echo "学分必须大于0";
        exit;
    }
    
    $sql = "UPDATE courses SET 
            course_name = '$course_name', 
            teacher_id = " . ($teacher_id ? $teacher_id : "NULL") . ", 
            credit = $credit 
            WHERE course_id = $course_id";
            
    if ($conn->query($sql)) {
        header('Location: course_management21.php');
        exit;
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
?>

<!DOCTYPE html>
<html lang="zh-hans">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改课程信息</title>
    <link rel="stylesheet" href="style.css">
    <style>
        form {
            max-width: 500px;
            margin: 20px auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="text"],
        input[type="number"],
        select {
            width: 100%;
            padding: 8px;
            margin-bottom: 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        input[type="submit"] {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            width: 100%;
        }
        input[type="submit"]:hover {
            background-color: #0056b3;
        }
        .back-link {
            display: block;
            text-align: center;
            margin-top: 20px;
            color: #007bff;
            text-decoration: none;
        }
        .back-link:hover {
            text-decoration: underline;
        }
        .form-group {
            margin-bottom: 15px;
        }
    </style>
</head>
<body>
    <h2>修改课程信息</h2>
    <form action="<?php echo $_SERVER['PHP_SELF'] . "?id=$course_id" ?>" method="post">
        <div class="form-group">
            <label for="course_name">课程名称:</label>
            <input type="text" name="course_name" value="<?php echo $row['course_name']; ?>" required>
        </div>
        
        <div class="form-group">
            <label for="teacher_id">授课教师:</label>
            <select name="teacher_id">
                <option value="">-- 选择教师 --</option>
                <?php
                while ($teacher = $teachers_result->fetch_assoc()) {
                    $selected = ($teacher['teacher_id'] == $row['teacher_id']) ? 'selected' : '';
                    echo "<option value='" . $teacher['teacher_id'] . "' $selected>" . 
                         $teacher['teacher_name'] . "</option>";
                }
                ?>
            </select>
        </div>
        
        <div class="form-group">
            <label for="credit">学分:</label>
            <input type="number" name="credit" min="1" value="<?php echo $row['credit']; ?>" required>
        </div>
        
        <input type="submit" name="edit_course" value="修改">
    </form>
    <a href="course_management21.php" class="back-link">返回课程管理页面</a>
</body>
</html>